package com.msb.user_consumer;

import com.msb.user_api.IUserApi;
import com.msb.user_api.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class UserController  {

    @Autowired
    UserApi userApi;
    /*@Autowired
    IUserApi userApi;*/

    private AtomicInteger count = new AtomicInteger();

    @GetMapping("/alive")
    public Object alive(){

        return userApi.alive();
    }

    @GetMapping("/map")
    Map<Integer, String> getMap(@RequestParam("id") Integer id){
        System.out.println(id);
        return userApi.getMap(id);
    }

    /**
     * 生产者和消费者的参数注解都是可以省略的，但是FeignClient的不可以
     * @param id
     * @param name
     * @return
     */
    @GetMapping("/map2")
    public Map<Integer, String> getMap2(@RequestParam("id") Integer id,@RequestParam("name") String name) {
        System.out.println(id);
        return userApi.getMap2(id,name);
    }

    /**
     * @RequestParam 不可以删除
     * @param map
     * @return
     */
    @GetMapping("/map3")
    public Map<Integer, String> getMap3(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        return userApi.getMap3(map);
    }

    @GetMapping("/postMap")
    public Map<Integer,String> postMap(@RequestParam  Map<String,Object> map){
        System.out.println(map);
        return userApi.postMap(map);
    }

    @GetMapping("/postPerson")
    public Person postPerson(@RequestParam  Map<String,Object> map){
        System.out.println(map);
        Person person = new Person();
        person.setId(Integer.valueOf(map.get("id").toString()));
        person.setName(map.get("name").toString());
        return userApi.postPerson(person);
    }
}
